const logTypes = ['log', 'dir', 'error', 'info', 'warn', 'table', 'count'];
const colorLists = {
    'red':'#f60f0f',
    'black':'#101010'
}
const isObject = (data) => {
    let type = Object.prototype.toString.call(data);
    return type.split(' ')[0].slice(1) === 'object';
}
const operation = (type) => {
    return (value, options) => {
        let defaultOptions = {
            suffix:false,
            color:'black',
            background:'transparents',
            size:'16px'
        }
        if (!options)
        {
            return console[type](value);
        }
        Object.assign(defaultOptions, options);
        let color = colorLists[defaultOptions.color] || defaultOptions.color;
        let background = colorLists[defaultOptions.background] || defaultOptions.background;
        let fontSize = defaultOptions.size.endsWith('px') ? defaultOptions.size : `${defaultOptions.size}px`;
        if (defaultOptions.suffix)
        {
            if (isObject(value)) return console[type](value, `${defaultOptions.suffix}`);
            console[type](`%c${value}`,
                `color:${color};background:${background};font-size:${fontSize}`,
                `${defaultOptions.suffix}`);
        }
        else
        {
            if (isObject(value)) return console[type](value);
            console[type](`%c${value}`,
                `color:${color};background:${background};font-size:${fontSize}`);
        }
    }
}
class LogUtils {
    constructor () {
        logTypes.forEach((item) => {
            this[item] = operation(item);
        });
    }

    type (value) {
        if (!logTypes.includes(value)) throw new Error('log类型参数不正确')
        this.log_type = value;
        return this;
    }

    text (value) {
        this.log_text = value;
        return this;
    }

    options (value) {
        this.log_options = {
            suffix:false,
            color:'black',
            background:'transparents',
            size:'16px'
        };
        Object.assign(this.log_options, value);
        return this;
    }

    cout () {
        if (!this.log_options)
        {
            return console[this.log_type](this.log_text);
        }
        let defaultOptions = this.log_options;
        let color = colorLists[defaultOptions.color] || defaultOptions.color;
        let background = colorLists[defaultOptions.background] || defaultOptions.background;
        let fontSize = defaultOptions.size.endsWith('px') ? defaultOptions.size : `${defaultOptions.size}px`;
        if (defaultOptions.suffix)
        {
            if (isObject(this.log_text))
            {
                return console[this.log_type](this.log_text, `${defaultOptions.suffix}`);
            }
            console[this.log_type](`%c${this.log_text}`,
                `color:${color};background:${background};font-size:${fontSize}`,
                `${defaultOptions.suffix}`);
        }
        else
        {
            if (isObject(this.log_text))
            {
                return console[this.log_type](this.log_text);
            }
            console[this.log_type](`%c${this.log_text}`,
                `color:${color};background:${background};font-size:${fontSize}`);
            
        }
    }
}

export default new LogUtils();